﻿function CountryViewModel(parent) {
    var self = this;

    // knockout variables
    self.countries = ko.observableArray([]);
    self.workingCountry = ko.observable(null);

    //PAGING + SEARCH
    self.search = ko.observable("");
    self.currentPage = ko.observable(0);
    self.pageSize = ko.observable(10);
    self.pagesCount = ko.observable(1);
    self.count = ko.observable(0);
    self.lastPage = function() {
        self.currentPage(self.pagesCount() - 1);
    };
    self.firstPage = function() {
        self.currentPage(0);
    };
    self.nextPage = function() {
        if (self.currentPage() < self.pagesCount() - 1) {
            self.currentPage(self.currentPage() + 1);
        }
        ;
    };
    self.previousPage = function() {
        if (self.currentPage() > 0) {
            self.currentPage(self.currentPage() - 1);
        }
    };

    //SELECT COUNTRY
    self.selectWorkingCountry = function(country) {
        self.workingCountry(country);
    };
    self.deselectCountry = function() {
        self.workingCountry(null);
    };
    self.selectCountry = function(country) {
        self.selectWorkingCountry(country);
    };
    self.selectNewCountry = function() {
        var newCountry = new Country({ });
        self.countries.push(newCountry);
        self.selectWorkingCountry(newCountry);
    };

    //CRUD OPERATIONS
    self.saveNewCountry = function() {
        if (self.workingCountry().errors().length == 0) {
            $.ajax({
                    url: "/Country/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingCountry()),
                    contentType: 'application/json',
                    success: function(id) {
                        self.workingCountry().Id(id);
                        self.deselectCountry();
                        $("#CreateCountry").modal("hide");
                    }
                });
        }
    };
    self.saveEditCountry = function() {
        if (self.workingEducation().errors().length == 0) {
            $.ajax({
                    url: "/Country/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingCountry()),
                    contentType: 'application/json',
                    success: function() {
                        self.deselectCountry();
                        $("#EditCountry").modal("hide");
                    }
                });
        }
    };
    self.deleteCountry = function() {
        $.post("/Country/DeleteCountry", { id: self.workingCountry().Id() }, function() {
            self.countries.pop(self.workingCountry());
            self.deselectCountry();
        });
        $('#DeleteCountry').modal('hide');
    };
    self.cancelNewCountry = function() {
        self.deselectCountry();
        self.countries.pop(self.workingCountry);
        $('#CreateCountry').modal('hide');
    };
    self.cancelEditCountry = function() {
        self.loadCountries();
        self.deselectCountry();
        $('#EditCountry').modal('hide');
    };
    self.cancelDetailsCountry = function() {
        self.deselectCountry();
        $('#DetailsCountry').modal('hide');
    };
    self.cancelDeleteCountry = function() {
        self.deselectCountry();
        $('#DeleteCountry').modal('hide');
    };

    self.loadCountries = function(pageIndex, pageSize, search) {
        $.getJSON("/Country/List", { search: search, pageIndex: pageIndex, pageSize: pageSize }, function(countries) {
            self.pagesCount(countries.PagesCount);
            self.count(countries.Count);
            var mappedCountries = $.map(countries.Entities, function (item) { return new Country(item); });
            self.countries(mappedCountries);
        });
    };
    self.triggerLoadCountries = ko.computed(function() {
        self.currentPage();
        self.pageSize();
        var search = self.search();
        if (parent.route() == "countries") {
            var route = '#/Countries/List/?pageIndex=' + self.currentPage() + '&pageSize=' + self.pageSize();
            if (search != null && search != "") {
                route = route + '&search=' + self.search();
            }
            location.hash = route;
        }
    }, this);

}